package com.whfc.iot.mach.processor;

import com.whfc.common.util.JSONUtil;
import com.whfc.hdc.dto.DeviceUpgradeDataDTO;
import com.whfc.iot.mach.constant.MsgConst;
import com.whfc.iot.mach.entity.MachMsg0x02;
import com.whfc.iot.mach.entity.MachMsg0x82;
import com.whfc.iot.mach.service.DeviceUpgradeService;
import com.whfc.iot.session.Session;
import com.whfc.iot.session.SessionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description: 0x02消息处理
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/8/23 14:25
 */
@Component
public class MachUpMsgProcessor0x02 implements MachUpMsgProcessor<MachMsg0x02> {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DeviceUpgradeService deviceUpgradeService;

    @Autowired
    private SessionManager sessionManager;

    @Override
    public void processMsg(MachMsg0x02 reqMsg) {

        logger.info("处理0x02指令:{}", JSONUtil.toString(reqMsg));
        Integer deviceId = reqMsg.getDeviceId();
        Integer deviceType = reqMsg.getDeviceType();

        //转换数据
        int masterOrSlave = reqMsg.getMasterOrSlave();
        String ver = reqMsg.getVer();
        if (MsgConst.ACK_SUCCESS == reqMsg.getResult()) {
            int subPackageTotal = reqMsg.getSubPackageTotal();
            int subPackageSeq = reqMsg.getSubPackageSeq();
            if (subPackageSeq < subPackageTotal) {
                DeviceUpgradeDataDTO upgradeDataDTO = deviceUpgradeService.getDeviceUpgradeData(deviceType, masterOrSlave, ver, ++subPackageSeq);
                if (upgradeDataDTO != null) {
                    MachMsg0x82 resMsg = new MachMsg0x82();
                    resMsg.setStartFlag(MsgConst.START_FLAG);
                    resMsg.setDeviceType(deviceType);
                    resMsg.setDeviceId(deviceId);
                    resMsg.setSeq(reqMsg.getSeq() + 1);
                    resMsg.setCmd(MsgConst.DOWNSTREAM_CMD_0x82);
                    resMsg.setMasterOrSlave(masterOrSlave);
                    resMsg.setVer(ver);
                    resMsg.setSubPackageTotal(upgradeDataDTO.getSubPackageTotal());
                    resMsg.setSubPackageSeq(upgradeDataDTO.getSubPackageSeq());
                    resMsg.setSubPackageData(upgradeDataDTO.getSubPackageData());
                    //响应0x82
                    Session session = sessionManager.getSessionByDeviceId(deviceId);
                    session.getChannel().writeAndFlush(resMsg).addListener(future -> {
                        logger.info("0x82发送结果,deviceId={},isDone={},isSuccess={}", deviceId, future.isDone(), future.isSuccess());
                    });
                }
            }
            //升级完毕
            else if (subPackageSeq == subPackageTotal) {
                logger.info("硬件升级完毕,deviceId:{},ver:{}", deviceId, ver);
                //deviceUpgradeService.upgradeComplete(deviceId);
            }
            //更新升级进度
            deviceUpgradeService.updateUpgradeProgress(
                    reqMsg.getDeviceId(),
                    reqMsg.getMasterOrSlave(),
                    reqMsg.getVer(),
                    reqMsg.getSubPackageSeq(),
                    reqMsg.getSubPackageTotal());
        }
        if (MsgConst.ACK_FAIL == reqMsg.getResult()) {

        }
    }
}
